package sy.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysProjectOwnerMappingDaoI;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.model.SysProjectOwnerMapping;
import sy.util.Menu;

@Repository("sysProjectOwnerMappingDao")
public class SysProjectOwnerMappingDaoImpl implements SysProjectOwnerMappingDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysProjectOwnerMapping sysProjectOwnerMapping) {
		return getCurrentSession().save(sysProjectOwnerMapping);//直接用
		
	}

	@Override
	public void remove(SysProjectOwnerMapping sysProjectOwnerMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysProjectOwnerMapping);
	}

	/* (non-Javadoc)
	 * @see sy.dao.sysProjectRoleMappingDaoI#update(sy.model.sysProjectRoleMapping)
	 */
	@Override
	public void update(SysProjectOwnerMapping sysProjectOwnerMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysProjectOwnerMapping);
	}

	/* (non-Javadoc)
	 * @see sy.dao.sysProjectRoleMappingDaoI#list()
	 */
	@Override
	public List<SysProjectOwnerMapping> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysProjectOwnerMapping").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectOwnerMappingDaoI#getSysProjectOwnerMappingById(java.lang.String)
	 */
	@Override
	public SysProjectOwnerMapping getSysProjectOwnerMappingById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysProjectOwnerMapping where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysProjectOwnerMapping> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectOwnerMappingDaoI#listSysProjectOwnerMappingByKey(java.lang.String)
	 */
	@Override
	public List<SysProjectOwnerMapping> listSysProjectOwnerMappingByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysProjectOwnerMapping where sysRole.id like :key or sysProject.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysProjectOwnerMapping> list = query.list();
		return list;
	}

	@Override
	public List<SysProjectOwnerMapping> listSysProjectOwnerMappingByOwnerId(
			String ownerId) {
		// TODO Auto-generated method stub
		String hql = "from SysProjectOwnerMapping where sysAccount.id = :ownerId";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("ownerId", ownerId);
		List<SysProjectOwnerMapping> list = query.list();
		return list;
	}
	
	@Override
	public Serializable saveSysProjectOwnerMappings(List<SysProjectOwnerMapping> sysProjectOwnerMapping) {//批量增加实体
		// TODO Auto-generated method stub
		Session session = this.getCurrentSession();
		SysProjectOwnerMapping projectOwner = null;
		for(int i = 0; i<sysProjectOwnerMapping.size(); i++){
			projectOwner = sysProjectOwnerMapping.get(i);
			session.save(projectOwner);
			if (i % 10 == 0) {  
				session.flush();  
				session.clear();  
			}  
		}
		return null;
	}

}
